package com.lbmb.mdps.workbench.controller;

import com.lbmb.mdps.commons.constant.Constant;
import com.lbmb.mdps.commons.domain.MedicineTypeVo;
import com.lbmb.mdps.commons.domain.ReturnObjectVO;
import com.lbmb.mdps.commons.domain.MedicineTypeReceive;
import com.lbmb.mdps.commons.exception.MedicineTypeException;
import com.lbmb.mdps.commons.utils.DateTimeUtil;
import com.lbmb.mdps.commons.utils.SnowFlakeIdUtil;
import com.lbmb.mdps.workbench.domain.MedicineType;
import com.lbmb.mdps.workbench.service.MedicineTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-05-30 23:11
 * description
 */
@CrossOrigin
@RestController
public class MedicineTypeController {

    @Resource
    private MedicineTypeService medicineTypeService;

    @PostMapping( "/workbench/medicineType/medicineTypePageList.do")
    public ReturnObjectVO medicineTypePageList(@RequestBody MedicineTypeReceive medicineTypeReceive) throws MedicineTypeException {
        /*
         * 分页获取药品类别列表
         * @author 林文塨
         * @date 2022/5/30 23:15
         * @param [medicineTypeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        medicineTypeReceive.setSkipCount((medicineTypeReceive.getPageNo()-1)*medicineTypeReceive.getPageSize());

        List<MedicineType> medicineTypeList = medicineTypeService.medicineTypePageList(medicineTypeReceive);

        rov.setOthers(medicineTypeList);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/medicineType/getMedicineTypeTotal.do")
    public ReturnObjectVO getMedicineTypeTotal(@RequestBody MedicineTypeReceive medicineTypeReceive) throws MedicineTypeException {
        /*
         * 获取药品类别分页总条数功能
         * @author 林文塨
         * @date 2022/5/30 10:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        Integer total = medicineTypeService.getMedicineTypeTotal(medicineTypeReceive);
        rov.setNumber(total);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/medicineType/addMedicineType.do")
    public ReturnObjectVO addMedicineType(@RequestBody MedicineType medicineType) throws MedicineTypeException {
        /*
         * 添加药品类别
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        medicineType.setCreateTime(DateTimeUtil.getSysTime());
        medicineType.setCreateBy(medicineType.getCurrentUserId());
        medicineType.setDeleteFlag("0");
        medicineType.setId(SnowFlakeIdUtil.generateId());

        medicineTypeService.addMedicineType(medicineType);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/medicineType/editMedicineType.do")
    public ReturnObjectVO editMedicineType(@RequestBody MedicineType medicineType) throws MedicineTypeException {
        /*
         * 修改药品类别
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        medicineType.setEditTime(DateTimeUtil.getSysTime());
        medicineType.setEditBy(medicineType.getCurrentUserId());

        medicineTypeService.editMedicineType(medicineType);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/medicineType/deleteMedicineType.do")
    public ReturnObjectVO deleteEmployee(@RequestBody String [] deleteMedicineTypeIds) throws MedicineTypeException {
        /*
         * 删除药品类别
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        medicineTypeService.deleteMedicineType(deleteMedicineTypeIds);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @GetMapping( "/workbench/medicineType/getMedicineTypeData.do")
    public ReturnObjectVO getMedicineTypeData() {
        /*
         * 获取全部药品类别
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        List<MedicineTypeVo> medicineTypeData = medicineTypeService.getAllMedicineType();
        /*ServletContext servletContext = session.getServletContext();
        Object medicineTypeData = servletContext.getAttribute(Constant.MEDICINE_TYPE_DATA);*/

        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
        rov.setOthers(medicineTypeData);

        return rov;
    }
}
